package com.sea.base.ext.global

import android.content.res.Resources

inline val Number.dpF get():Float = this.toFloat() * Resources.getSystem().displayMetrics.density
inline val Number.dp get():Int = (this.dpF + 0.5f).toInt()

/**
 * 屏幕宽的百分比
 * 对于ui图，可以把ui宽指定为1000像素来查看百分比
 */
inline val Number.screenWidthRatio get():Int = this.screenWidthRatioF.toInt()
inline val Number.screenWidthRatioF get():Float = this.toFloat() * SCREEN_WIDTH

/***
 * 屏幕宽高
 */
val SCREEN_WIDTH = Resources.getSystem().displayMetrics.widthPixels
val SCREEN_HEIGHT = Resources.getSystem().displayMetrics.heightPixels

inline val Number.KB get():Long = this.KBF.toLong()
inline val Number.KBF get():Float = this.toFloat() * 1024f
inline val Number.MB get():Long = this.MBF.toLong()
inline val Number.MBF get():Float = this.toFloat() * 1.KBF * 1.KBF
inline val Number.GB get():Long = this.GBF.toLong()
inline val Number.GBF get():Float = this.toFloat() * 1.MBF * 1.KBF

/**
 * 资源字符串
 */
inline val Int.resString get() = singleApplication.getString(this)

@Suppress("NOTHING_TO_INLINE")
inline fun getCurrentMillis() = System.currentTimeMillis()

@Suppress("NOTHING_TO_INLINE")
inline fun getNanoTime() = System.nanoTime()

/**
 * try后仅打印
 */
inline fun <T> runCatchingPrint(block: () -> T): T? {
    return try {
        block.invoke()
    } catch (e: Throwable) {
        e.printStackTrace()
        null
    }
}